package com.ztrue.framework.infrastructure.impl;

import com.ztrue.framework.infrastructure.IClassProvider;

import java.util.HashMap;
import java.util.Map;

/**
 * 默认类型和类型名称映射处理
 *
 * @author zhangw
 * @date 2016/10/27 16:41
 * @version: 1.0
 */
public class DefaultClassProvider implements IClassProvider {
    private Map<String, Class> cache = new HashMap<String, Class>();

    /**
     * 获取Class名称
     *
     * @param cls Class对象
     * @return
     */
    public String getName(Class cls) {
        return cls.getName();
    }

    /**
     * 通过Class名称获取Class对象
     *
     * @param className Class名称
     * @return
     */
    public Class getClass(String className) throws ClassNotFoundException {
        Class cls = cache.get(className);
        if (cls != null) return cls;
        cls = Class.forName(className);
        if (cls != null) cache.put(className, cls);
        return cls;
    }
}
